//
//  AppDelegate.swift
//  WeiBo
//
//  Created by DDC on 2017/9/6.
//  Copyright © 2017年 DDC. All rights reserved.
//

import UIKit
import UserNotifications
import SVProgressHUD
import AFNetworking
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
    {
        
        //设置额外信息
        setUpAdditions()
        
        
//        sleep(1)
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white
        window?.rootViewController = JCLMainViewController()
        window?.makeKeyAndVisible()
        
        
        loadDataInfo()
        return true
    }

    
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

// MARK: - 设置额外信息
extension AppDelegate {
    
    fileprivate func setUpAdditions () {
        //设置提示框显示最小时间
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        //开启网络提示器
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //应用程序授权
        //申请授权设备 显示badge iOS 10以上
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert,.badge,.sound,.carPlay]) { (success, error) in
                print("授权" + (success ? "成功"  : "失败"))
            }
        } else {
            
            //申请授权设备 显示badge iOS 10以下
            let userNotifySettings = UIUserNotificationSettings(types: [.alert,.badge,.sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(userNotifySettings)
        }
        
    }
}

extension AppDelegate {
    
    fileprivate func loadDataInfo(){
        
        DispatchQueue.global().async {
            
            let path = Bundle.main.url(forResource: "data.json", withExtension: nil)
            
            let data = NSData.init(contentsOf: path!)
//            let data = try! Data(contentsOf: path!)
            
            //写入磁盘
            let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
            
            //拼接文件名
            let jsonPath = (docDir as NSString).appendingPathComponent("data.json")
            
            //存入数据到沙盒
            data?.write(toFile: jsonPath, atomically: true)
            
            
            print("应用程序加载完毕\(jsonPath)")
        }
        
        
    }
}
